package com.guigu.servicesystem.security.config.security.handler;


import com.alibaba.fastjson.JSONObject;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicesystem.security.model.bean.ResultJSON;
import com.guigu.servicesystem.security.utils.SecurityHandlerUtil;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.guigu.servicesystem.security.utils.args.ResultArgsUtil.AUTHORIZE_FAILURE_CODE;
import static com.guigu.servicesystem.security.utils.args.ResultArgsUtil.AUTHORIZE_FAILURE_MSG;


/**
 * @Author xiumu
 * @Date 2020/5/28 20:13
 */
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, org.springframework.security.access.AccessDeniedException e) throws IOException, ServletException {
        System.out.println("进入了AccessDeniedHandlerImpl方法++++++++++用来解决认证过的用户访问无权限资源时的异常");
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setContentType("text/javascript;charset=utf-8");
        httpServletResponse.getWriter().print(JSONObject.toJSONString(Result.error().message("没有访问权限!")));
        SecurityHandlerUtil.responseHandler(httpServletResponse,new ResultJSON(AUTHORIZE_FAILURE_CODE,AUTHORIZE_FAILURE_MSG));
    }
}
